﻿// See https://aka.ms/new-console-template for more information

using System.Diagnostics;
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Vin.DependencyInjection.Core;
using Vin.Extension.Workflow.Node;
using Vin.Extension.Workflow.Node.Dto;
using Vin.Extension.Workflow.Service;
using Vin.Extension.Workflow.Tools;
using Vin.Tool.AspNetCore.Singleton;
using Vin.Tool.Core.JsonCategory;
using Vin.Workflow.Tests;
using NodeResult = Vin.Extension.Workflow.Node.Models.NodeResult;

var builder = Host.CreateEmptyApplicationBuilder(null);

builder.UseVin();

builder.Services.AddSingleton<IAsyncNodeResultProvider, AsyncNodeResultProvider>();

var app = builder.Build();

app.UseVin();

// Test.ServiceProvider = app.Services;
// Test.Main();

// var type = System.Type.GetType("Vin.Extension.Workflow.Models.NodeResult, Vin.Extension.Workflow");
// var result = Activator.CreateInstance(type!) as NodeResult;

await VinApp.CreateScopeAsync(async (_) =>
{
    var runnerService = app.Services.GetRequiredService<RunnerService>();
    var nodeResultDict = NodeManager.NodeResult;
    nodeResultDict.Clear();
    var res = await runnerService.RunAsync("", TestNodes.ForNode);
    Console.WriteLine($@"单节点执行完成:{res.ToJson()}");
});

// await VinApp.CreateScopeAsync(async (_) =>
// {
//     var runnerService = app.Services.GetRequiredService<RunnerService>();
//     var nodeResultDict = NodeManager.NodeResult;
//     nodeResultDict.Clear();
//     var res = await runnerService.RunAsync(TestNodes.PlaywrightWorkFlow);
//     Console.WriteLine($@"工作流执行完成:{res.ToJson()}");
// });

// await VinApp.CreateScopeAsync(async (_) =>
// {
//     var runnerService = app.Services.GetRequiredService<RunnerService>();
//     var res = await runnerService.RunAsync(TestNodes.DeserializeNode);
//     Console.WriteLine($@"单节点执行完成:{res.ToJson()}");
// });

//
// // 读取根目录的json
// var jsonPath = Path.Combine(AppContext.BaseDirectory, "test2.json");
// var workflow = await WorkflowTool.Load(jsonPath);
//
// Task.Run(async () =>
// {
//     try
//     {
//         NodeManager.AsyncNodeResultProvider.NodeResult.Add("开始", new());
//         var runnerService = app.Services.GetRequiredService<RunnerService>();
//         var res = await runnerService.RunAsync(new NodeDto()
//         {
//             Id = "1",
//             Name = "http test",
//             ServiceKey = "vin-http-node",
//             InputParameters = new List<NodeParameterDto>()
//             {
//                 new()
//                 {
//                     Key = "url",
//                     Value = "https://www.baidu.com",
//                     Type = ParameterType.String,
//                 },
//                 new()
//                 {
//                     Key = "method",
//                     Value = "GET",
//                     Type = ParameterType.String
//                 }
//             }
//         });
//         var t = res.ToJson();
//         Console.WriteLine($@"单节点执行完成:{res.ToJson()}");
//         // var result = await runnerService.RunAsync(workflow);
//         // // await Task.Delay(500);
//         // var json = NodeManager.AsyncNodeResultProvider!.NodeResult.ToJson();
//         // Console.WriteLine(
//         //     $@"入口函数执行完成:{NodeManager.AsyncNodeResultProvider!.NodeResult.ToJson()}");
//     }
//     catch (Exception e)
//     {
//         Console.WriteLine(e);
//         throw;
//     }
// });

// VinApp.CreateScopeAsync(async (_) =>
// {
//     var runnerService = VinApp.GetRequiredServiceByHttpContext<RunnerService>();
//     var result = await runnerService.RunAsync(workflow);
//     await Task.Delay(500);
//     Console.WriteLine(
//         $@"入口函数执行完成:{WorkflowManager.AsyncNodeResultProvider!.NodeResult.ToJson()}");
// });


// VinApp.CreateScopeAsync(async (_) =>
// {
//     var runnerService = app.Services.GetRequiredService<RunnerService>();
//     var result = await runnerService.RunAsync(workflow);
//     await Task.Delay(500);
//     Console.WriteLine(
//         $@"入口函数执行完成:{VinApp.GetRequiredServiceByHttpContext<NodeResult>().ToJson()}");
// });

// VinApp.CreateScopeAsync(async (_) =>
// {
//     var a = VinApp.GetRequiredServiceByHttpContext<NodeResult>();
//     var runnerService = app.Services.GetRequiredService<RunnerService>();
//     var result = await runnerService.RunAsync(workflow);
//     await Task.Delay(500);
//     Console.WriteLine(
//         $@"{Thread.CurrentThread.ManagedThreadId}:{a.ToJson()}");
// });

for (var i = 0; i < 10; i++)
{
    var d = i;
    // VinApp.CreateScopeAsync(async (_) =>
    // {
    //     var runnerService = app.Services.GetRequiredService<RunnerService>();
    //     var result = await runnerService.RunAsync(workflow);
    //     await Task.Delay(500);
    //     Console.WriteLine(
    //         $@"{Thread.CurrentThread.ManagedThreadId}:{VinApp.GetRequiredServiceByHttpContext<NodeResult>().ToJson()}");
    // });
    // Task.Run(async () =>
    // {
    //     WorkflowManager.AsyncNodeResultProvider.NodeResult.Add("开始" + d, new());
    //     var runnerService = app.Services.GetRequiredService<RunnerService>();
    //     var result = await runnerService.RunAsync(workflow);
    //     await Task.Delay(500);
    //     Console.WriteLine(
    //         $@"入口函数执行完成:{WorkflowManager.AsyncNodeResultProvider!.NodeResult.ToJson()}");
    // });
}

// Console.WriteLine("for循环结束");

// await Task.WhenAll(task(), task2(), task3(), task4());

Console.ReadKey();

// {
//     var json = await File.ReadAllTextAsync(jsonPath);
//     var document = JsonDocument.Parse(json);
//     var dict = document.RootElement.EnumerateObject()
//         .ToDictionary(m => m.Name, m => m.Value);
//     var te = 1;
// }

// async Task<string> Run1()
// {
//     await Task.Delay(5000);
//     return "Run1";
// }
//
// async Task<string> Run2()
// {
//     await Task.Delay(3000);
//     return "Run2";
// }
//
// var list = new List<WaitRunnerTaskTest>();
// list.Add(new WaitRunnerTaskTest("1", Run1()));
// list.Add(new WaitRunnerTaskTest("2", Run2()));
//
// // 记时间
// var sw = new Stopwatch();
// sw.Start();
//
// await Task.WhenAll(list.Select(m => m.RunTask));
//
// sw.Stop();
//
// Console.WriteLine($"总耗时: {sw.ElapsedMilliseconds}ms");
//
// foreach (var item in list)
// {
//     Console.WriteLine($"{item.Key}:{item.RunTask.Result}");
// }

// // 读取根目录的json
// var jsonPath = Path.Combine(AppContext.BaseDirectory, "test1.json");
// var nodes = await NodeTool.LoadNodes(jsonPath);